package com.protocolterminal;

import java.util.ArrayList;
import java.util.List;

import com.protocolterminal.AFN.BaseAFN;
import com.protocolterminal.DataField.GetDataField_3761;
import com.protocolterminal.DataField.GetDataField_STS;
import com.protocolterminal.DataField.GetDataField_abroad;
import com.protocolterminal.DataField.GetDataField_water;
import com.protocolterminal.DataField.GetDataField;
import com.protocolterminal.base.Control;
import com.protocolterminal.base.Seq;
import com.protocolterminal.frame.Frame3761;
import com.protocolterminal.function.Check3761;
import com.protocolterminal.function.CheckFrame;

/**
 * @ClassName: ProtocolServer
 * @Description: 协议的服务类，所有对外接口均在这里体现
 * @author baozhijian
 * @date 2017年9月8日 下午1:50:08
 * @version V1.0
 */
public class ProtocolTerminalService {

	private CheckFrame checkFrame;

	public boolean Check(String frame) {
		this.checkFrame = new Check3761();
		return checkFrame.Check(frame);
	}

	// 集中器协议，表协议,相关参数
	public String Encode(int terminalprotocol, String AFN, String SEQ, List<String> DA, List<String> DT,
			List<String> Data, String AUX, int PRM, int functioncode, String address) {
		// 组数据域
		GetDataField getDataField = null;
		switch (terminalprotocol) {
		case 1:// 国网集中器
			getDataField = new GetDataField_3761();
			break;
		case 2:// 水表集中器
			getDataField = new GetDataField_water();
			break;
		case 3:// 海外集中器
			getDataField = new GetDataField_abroad();
			break;
		case 4:// sts集中器
			getDataField = new GetDataField_STS();
			break;
		default:
			getDataField = new GetDataField_3761();
			break;
		}

		// 计算da
		List<String> DA1 = new ArrayList<>();
		for (String string : DA) {
			if (string.equals("0000") || string.equalsIgnoreCase("00FF")) {
				DA1.add(string);
			} else {
				int ida = Integer.valueOf(string);

				String da = "";
				int i = ida / 8;
				int j = ida % 8;

				if (j == 0) {
					da = "80" + Integer.toHexString((i) | 0x100).substring(1);
				} else {
					da = Integer.toHexString((1 << (j - 1)) | 0x100).substring(1)
							+ Integer.toHexString((i + 1) | 0x100).substring(1);
				}

				DA1.add(da);
			}
		}

		String datafield = getDataField.Encode(AFN, SEQ, DA1, DT, Data, AUX);

		// 组3761帧
		Control control = new Control(0, PRM, 0, 0, functioncode);
		Frame3761 frame3761 = new Frame3761(control, address, datafield);

		return frame3761.toString();
	}

	public String Decode(int terminalprotocol, String sentdata, String returndata) {
		Frame3761 sentframe = new Frame3761(sentdata);
		Frame3761 returnframe = new Frame3761(returndata);

		// 判断帧序号
		Seq sentseq = new Seq(sentframe.getData().substring(2, 4));
		Seq returnseq = new Seq(returnframe.getData().substring(2, 4));

		if (sentseq.getPSEQ() != returnseq.getPSEQ()) {
			return "ERR:帧序号不对";
		}
		// 解析数据域
		GetDataField getDataField = null;
		switch (terminalprotocol) {
		case 1:// 国网集中器
			getDataField = new GetDataField_3761();
			break;
		case 2:// 水表集中器
			getDataField = new GetDataField_water();
			break;
		case 3:// 海外集中器
			getDataField = new GetDataField_abroad();
			break;
		case 4:// sts集中器
			getDataField = new GetDataField_STS();
			break;
		default:
			getDataField = new GetDataField_3761();
			break;
		}

	
		BaseAFN baseAFN = getDataField.Decode(returnframe.getData());

		String showdata = "";
		for (String data : baseAFN.getData()) {
			showdata += data + ";";
		}

		if (showdata.length() > 0) {
			showdata = showdata.substring(0, showdata.length() - 1);
		}

		return showdata;
	}

}
